<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
    <div class="layui-form layuimini-form"   lay-filter="example"  >
        <form  ng-app="myApp" ng-controller="customersCtrl" ng-cloak name="myForm">
            <div  class="layui-tab layui-tab-brief" >
                <ul class="layui-tab-title">
                <li class="layui-this"> 
                    <i class="layui-icon">基础设置</i>
                </li>
                <li  id="exampleis"  style="display: none;">
                    <i class="layui-icon">数据表设置</i>
                </li>
                </ul>
                <div class="layui-tab-content" >
                <div class="layui-tab-item layui-show"  >
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label required">项目名称</label>
                                <div class="layui-input-inline"> 
                                    <input type="text" name="name"  ng-model="project.name" lay-verify="required"  class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label required">项目key</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="key" id="key"  ng-model="project.key" lay-verify="required"  class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">选择模型</label>
                                <div class="layui-input-inline">
                                    <select  ng-model="project.model_id" name="model_id">
                                        <option ng-repeat="x in modelnames" value="{{$index}}" >{{x}}</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">状态</label>
                                <div class="layui-input-inline">
                                    <input type="checkbox" checked="" name="status" lay-skin="switch" lay-filter="status" lay-text="开启|关闭">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">图标</label>
                                <input type="hidden" name="small_pic"  ng-model="project.small_pic" id="small_pic" value="">
                                <input type="hidden" name="id"  ng-model="project.id" id="id" value="">
                                <div class="layui-input-inline">
                                    <div class="layui-upload">
                                        <button type="button" class="layui-btn layui-btn-primary" id="cateBtn">
                                            <i class="icon icon-upload3"></i>点击上传</button>
                                        <div class="layui-upload-list">
                                            <img class="layui-upload-img"  ng-src="{{project.small_pic}}" style="width: 200px;height: 100px;" >
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div> 
                        <div class="layui-form-item" id="up-dd" style="display: none;">
                            <div class="layui-input-block">
                                <!-- lay-filter="saveBtn" -->
                                <button class="layui-btn layui-btn-normal" lay-submit   ng-click="subForm()" >确认保存</button>
                            </div>
                        </div>
                </div>
                <div class="layui-tab-item" >
                        <div class="layui-inline">
                            <div class="layui-input-block">
                                <table class="layui-table">
                                    <colgroup>
                                    <col width="150">
                                    <col width="200">
                                    <col>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>数据表</th>
                                        <th>表名称</th>
                                        <th>是否启用</th>
                                    </tr> 
                                    </thead>
                                    <tbody>
                                    <tr  ng-repeat="(key,table) in project.table  ">
                                        <td>{{table.title}}</td>
                                        <td>                                
                                            <input type="text" name="{{table.type}}"  ng-model="table.name" autocomplete="off" class="layui-input">
                                        </td>
                                        <td>
                                            <div class="layui-input-inline">
                                                <input type="checkbox" checked="" name="{{table.type}}_status" lay-skin="switch" lay-filter="status" lay-text="开启|关闭">
                                            </div>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <!-- lay-filter="saveBtn" -->
                                <button class="layui-btn layui-btn-normal" lay-submit   ng-click="subForm()" >确认保存</button>
                            </div>
                        </div>
                </div>
                </div>
            </div>
        </form>
        
    </div>
    
    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="../../js/lay-config.js" charset="utf-8"></script>
<script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function ($scope, $http,$location) {
        $scope.modelnames = ["普通", "连载", "电影", "连续剧","下载式"];
        $scope.project ={
            "name": '项目1',
            "status":1,
            "model_id":"0",
            "small_pic":"",
            "key":"XD"+Number((Math.random() * 10000).toFixed(0)),
            "table":[
                {"type":"essay","title":"文章表","status":1,"name":""},
                {"type":"essay_chapter","title":"文章副表","status":1,"name":""},
                {"type":"class","title":"分类","status":1,"name":""},
                {"type":"keyword","title":"关键词","status":1,"name":""},
                {"type":"likeit","title":"点赞","status":1,"name":""},
                {"type":"read","title":"阅读记录","status":1,"name":""},
                {"type":"collection","title":"收藏","status":1,"name":""},
                {"type":"score","title":"评分","status":1,"name":""},
                {"type":"teacher","title":"人物","status":1,"name":""},
                {"type":"share","title":"分享","status":1,"name":""},
                ]
            };
        layui.use(['form','upload','http'], function () {
            var form = layui.form,
                layer = layui.layer,
                uploadInst = layui.upload,
                http  = layui.http,
                apiurl =http.api,
                $ = layui.$;
            http.verifyToken();
            var id  = http.getQueryString('id');
            if(id){
                getProject();
                $('#up-dd').show();
            }else{
                $("#exampleis").show();
            }
            
            function getProject(){
                http.httpajax("Personal/projectList",{'id':id},"POST",function(res) {
                    if(res.code == '0000'){
                        form.render();
                        form.val('example', res.data)
                        $('#small_pic').attr('src',res.data.small_pic);
                        $('#id').val(res.data.id);
                        $scope.project=res.data;
                        $scope.$apply();
                    }else{
                        layer.msg(res.msg,{time:1000,icon:2});
                    }
                });
            }


            $('input[name="key"]').change(function(e) {
                var value =e.delegateTarget.value;
                if(!id){
                    angular.forEach($scope.project.table, function (data, index, array) {
                        $scope.project.table[index].name =value+"_"+$scope.project.table[index].type;
                    });
                    $scope.$apply();
                }
            })

            form.on('switch(status)', function (data) {
                var v = this.checked ? 1 :0 ;
                if(data.elem.name =='status'){
                    $scope.project.status = v  ;
                }
                else{
                    angular.forEach($scope.project.table, function (da, index, array) {
                        if($scope.project.table[index].type+"_status" ==data.elem.name){
                            $scope.project.table[index].status =v;
                        }
                    });
                }
                $scope.$apply();
            });

            form.on("select", function(data){
                $scope.project.model_id = data.value;
                $scope.$apply();
            });

            $scope.subForm = function () {
                var str = angular.toJson( $scope.project);//str为字符串
                var abj = JSON.parse(str);//json为对象
                http.httpajax("Personal/projectAdData", abj,"POST",function(res) {
                    if(res.code === '0000'){
                        layer.msg(res.msg,{time:1000,icon:1},function(){
                            // 关闭弹出层
                            layer.close();
                            window.parent.location.reload();//刷新父页面
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    }else{
                        layer.msg(res.msg,{time:1000,icon:2});
                    }
                });
            };

            http.uplode("cateBtn",function(res) {
                if(res.code==200){
                    $scope.project.small_pic = res.data;
                    $scope.$apply();
                    // $('#cateImage').attr('src',res.data);
                    layer.msg("上传成功",{offset:['200px','450px'],icon:6});
                }else{
                    //如果上传失败
                    return layer.msg('上传失败');
                }
            });
        });
      
        

        
    });
</script>
</body>
</html>